﻿@{
    Layout = null;
    var imageServer = new Ec_System.Facade.Web.SiteConfigFacade().ImageServer;
    var siteConfig = new Ec_System.Facade.Web.SiteConfigFacade();
}
@model List<Ec_System.Models.Web.ShopCartData>

<script type="text/javascript">
    $(document).ready(function ($) {
        $(".A_blue").click(
            function () {
                var t = $(this).parent().find("[name='txt_Num']");
                t.val(parseInt(t.val()) + 1);
                setTotal()
            });
        $(".B_blue").click(
    function () {
        var t = $(this).parent().find("[name='txt_Num']");
        t.val(parseInt(t.val()) - 1);
        if (parseInt(t.val()) < 1)
            t.val(1);
        setTotal()
    });

        function setTotal()
        {
            var goodMoney = 0;
            $("#CartList").find("tr").each(function () {
                var tdArr = $(this).children();
                var unitPrice = tdArr.eq(2).find(".td_price").html();
                var cunt = tdArr.eq(3).find("[name='txt_Num']").val();
                var rowMoney = tdArr.eq(5).find("[name='rowMoney']");
                if (!isNaN(unitPrice) && !isNaN(cunt))
                {
                    rowMoney.html(unitPrice * cunt);
                    goodMoney += unitPrice * cunt;

                    computerTotalMoney(goodMoney);
                }

            });
        }

        function computerTotalMoney(goodMoney) {
            if(goodMoney > @siteConfig.ShipfreePrice){
                $("#shipPrice").html("0");
            $("#totalMoney").html(goodMoney);
            $("#spanMoney").html(goodMoney)
        }
    else{
                $("#shipPrice").html(@siteConfig.ShipPrice)
        $("#totalMoney").html(goodMoney + @siteConfig.ShipPrice)
        $("#spanMoney").html(goodMoney)
    }

    }
    setTotal();
    });


    function delItem(pID)
    {
        console.log(pID);
        $('<form id="tf" method="post" action="/ShopCart/DeleteShopCart" style="display:none;">@Html.AntiForgeryToken()<input type="hidden" name="pid" value="'+pID+'"/></form>')
        .appendTo('body').submit();
    }

    function ConfirmCar()
    {
        var ps="";
        $("#CartList").find("tr").each(function () {
            var tdArr=$(this).children();
            var pid=tdArr.eq(0).find("[name='pid']").val();
            var num=tdArr.eq(3).find("[name='txt_Num']").val();
            if(!isNaN(pid)&&!isNaN(num))
            {
                ps+='{pid:'+pid+',num:'+num+'},';
            }
        });
        if(ps!='')
        {
            $('<form id="tf" method="post" action="/ShopCart/ConfirmCar" style="display:none;">@Html.AntiForgeryToken()<input type="hidden" name="items" value="'+ps.toString()+'"/></form>')
            .appendTo('body').submit();
        }
    }

</script>

<div class="block clearfix">
    <div id="ECS_COMMENT">
        <div class="box_1">
            <div class="ur_here">
                <a href="">首页</a> <code>&gt;</code> 购物车 <code>&gt;</code> <a href="/ShopCart/Index">我的购物车</a>
            </div>
            <div class="h3_2">
                <div>
                    <img src="~/Content/images/car.jpg" style="float:left" width="30" height="30" /><h1>我的购物车</h1>
                </div>
            </div>
            <div class="blank"></div>
            <table id="CartList" class="tbCartList">
                <th>商品</th>
                <th>标题</th>
                <th>单价</th>
                <th>数量</th>
                <th>单位</th>
                <th>小计</th>
                <th>删除</th>
                @if (Model != null)
                {
                    foreach (var item in Model)
                    {
                    <!--tr动态item.ProductID, 删除时根据ID删除Tr. 未完成的设想-->
                        <tr>
                            <td align="left" valign="middle" width="85">
                                <a href="/Item/@item.ProductID">
                                    <img src="@imageServer/product/6060@{@item.ImageUrl}" width="80" height="80" />
                                </a>
                                <input type="hidden" name="pid" value="@item.ProductID">
                            </td>
                            <td align="left" valign="middle">
                                <a href="/Item/@item.ProductID">@item.ShopProductTitle</a><br />
                            </td>
                            <td align="center" valign="middle">
                                <span class="td_price"> @item.ProductPrice.ToString("0.00")</span>
                            </td>
                            <td align="center" valign="middle">
                                <input class="B_blue" type="button" value=" - " />
                                <input class="ByNum_input" name="txt_Num" id="txt_Num" value="@item.ProductQty">
                                <input class="A_blue" type="button" value=" + " />
                                @if (item.StoreStatus == 0)
                                {
                                    <br />
                                    <span class="p-price">库存不足</span>
                                }

                            </td>
                            <td align="center" valign="middle">
                                @item.Unit
                            </td>
                            <td align="center" valign="middle">
                                ￥<span name="rowMoney" class="td_price">@item.RowMoney.ToString("0.00")</span>
                            </td>
                            <td align="center" valign="middle">
                                <a href="javascript:delItem(@item.ProductID)" title="删除"><img style="vertical-align:bottom;" src="~/Content/images/dang1.gif" /> </a>
                            </td>
                        </tr>
                    }           
                }

            </table>
            <div class="bottom_txt  in1 h3" style="text-align:right">
                共计：<span class="td_price">@{  if (Model != null) { @Model.Count} }</span>件商品，商品金额：￥<span id="spanMoney" class="td_price"></span> 元，邮费：￥<span id="shipPrice" class="td_price"></span>，应付金额：￥<span id="totalMoney" class="td_price"></span>元
                <input type="button" value="" class="btn_checkOut" onclick="ConfirmCar()" />
            </div>
        </div>
    </div>
</div>